import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

import com.xdtech.hotel.protocol.util.SocketUtil;

/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 * @author max.zheng
 * @create 2015-7-18下午9:28:00
 * @since 1.0
 * @see
 */
public class RCUDeviceServer {
	public static void main(String[] args) throws Exception {
		rcuServerRunning();
		
//		rcuServerComminting();
	}

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-7-19上午12:14:53
	 * @modified by
	 */
	private static void rcuServerComminting() {
		try
		{
			Socket socket = new Socket("192.168.1.105", 9001);
			BufferedReader in;
			PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
			
//			out.println("{\"SN\": \"123456789\",\"commit\": [{\"ac\": [{\"addr\": \"1\",\"channel\": \"1\",\"fan\": \"low\",\"mode\": \"auto\",\"power\":\"on\",\"temp\": \"13\"}]}]}");
			
			//顾客 请求退房
			out.println("{\"SN\":\"123456789\",\"commit\":[{\"room\":[{\"number\": \"16\"}]}]}"); 
			
			
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"dimmer\":[{\"addr\": \"4\",\"channel\":\"0\",\"status\": \"on\",\"freq\":\"0\",\"delay\":\"22\",\"value\": \"0\" }]}]}");
//			            out.println("{\"SN\":\"123456789\",\"commit\":[{\"fault\":[{\"addr\": \"2\",\"channel\":\"0\",\"type\": \"0\"}]}]}"); 
//			            out.println("{\"SN\":\"123456789\",\"commit\":[{\"scene\":[{\"number\": \"1\"}]}]}");
			//顾客
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"room\":[{\"number\": \"15\"}]}]}"); 
			//            line.close();  
			//节点
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"node\":[{\"addr\":\"6\",\"channel\":\"6\",\"status\":\"off\",\"freq\":\"0\",\"delay\":\"0\"}]}]}"); 
//			{"addr": "0","channel": "0","type": "0"},
			//故障
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"fault\": [{\"addr\" : \"1\",\"channel\" : \"1\",\"type\": \"0\"}]}]}");

			//卡commit
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"card\": [{\"status\" : \"off\",\"data\" : \"123\"}]}]}");

			System.out.println("服务端返回" + SocketUtil.readStrFromStream(socket
					.getInputStream()));
//			out.close();
//			in.close();

//			socket.close();

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @author max.zheng
	 * @create 2015-7-19上午12:05:36
	 * @modified by
	 * @throws IOException
	 * @throws SocketException
	 * @throws InterruptedException
	 */
	private static void rcuServerRunning() throws Exception {
		ServerSocket server = new ServerSocket(9000);
		

		while (true) {
			Socket request = server.accept();
			// set timeout:4 seconds
			request.setSoTimeout(30000);
			String reqStr = "";
			try {
				// if read() get a timeout exception
				reqStr = SocketUtil.readStrFromStream(request.getInputStream());
				System.out.println("RCU Device Receive data=" + reqStr);

				// write response info;do not response when heart breaking.
				if ("Heart break".equals(reqStr)) {
					SocketUtil.writeStr2Stream(
							"hello client, this is server. requestID : ",
							request.getOutputStream());
				} else {
//					SocketUtil
//							.writeStr2Stream(
//									"{\"SN\":\"123456789\",\"ack_w\" : [{\"node\": [{\"addr\":\"1\",\"channel\": \"1\",\"success\":\"true\"}]}]}",
//									request.getOutputStream());
					//空调调试
					SocketUtil.writeStr2Stream(
							"{\"SN\":\"123456789\",\"ack_w\" : [{\"ac\": [{\"addr\":\"1\",\"channel\": \"1\",\"success\":\"true\"}]}]}",
							request.getOutputStream());
					
					//音乐调试
//					SocketUtil.writeStr2Stream(
//							"{\"SN\":\"123456789\",\"ack_w\" : [{\"music\": [{\"addr\":\"2\",\"channel\": \"2\",\"success\":\"true\"}]}]}",
//							request.getOutputStream());
					
					//电视调试
//					SocketUtil.writeStr2Stream(
//							"{\"SN\":\"123456789\",\"ack_w\" : [{\"tv\": [{\"addr\":\"3\",\"channel\": \"3\",\"success\":\"true\"}]}]}",
//							request.getOutputStream());
					
					//调光调试
//					SocketUtil.writeStr2Stream(
//							"{\"SN\":\"123456789\",\"ack_w\" : [{\"dimmer\": [{\"addr\":\"4\",\"channel\": \"4\",\"success\":\"true\"}]}]}",
//							request.getOutputStream());
					
					//场景调试
//					SocketUtil.writeStr2Stream(
//							"{\"SN\":\"123456789\",\"ack_w\" : [{\"scene\": [{\"number\":\"1\",\"success\":\"true\"}]}]}",
//							request.getOutputStream());
					
				}
				request.close();
				Thread.sleep(1000);
			} catch (SocketTimeoutException e) {
				e.printStackTrace();
			}

		}
	}
}
